﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Demo15_1_2
{
    internal class Program
    {
        public static Person person = new Person();
        public static Student student = new Student();
        static void Main(string[] args)
        {
            Task.Run(() => {
                Console.WriteLine($"id={Environment.CurrentManagedThreadId}进入Person1");
                try {
                    Monitor.Enter(person);
                    Thread.Sleep(100);
                    var val = 10 / Convert.ToInt32("0");
                    Monitor.Exit(person);

                }
                catch (Exception ex) {

                    Console.WriteLine(ex.Message);
                }
                Console.WriteLine($"id={Environment.CurrentManagedThreadId}退出Person1");
            });

            Console.WriteLine("第二个线程开始进入");
            Thread.Sleep(10);
            Task.Run(() => {
               
                Console.WriteLine($"id={Environment.CurrentManagedThreadId}进入Person2");
                Monitor.Enter(person);
                Thread.Sleep(1000);
                Monitor.Exit(person);
                Console.WriteLine($"id={Environment.CurrentManagedThreadId}退出Person2");

            });
            Console.ReadLine();
        }
    }
    public class Person { }
    public class Student { }
}
